<template>
  <div class="player-list-container">
    <div class="section-header">
      <h3>玩家列表 ({{ players.length }}/{{ maxPlayers }})</h3>
      <div class="game-status" :class="gameStatus">
        {{ gameStatusText }}
      </div>
    </div>
    
    <div class="players-container">
      <div 
        v-for="player in sortedPlayers" 
        :key="player.id"
        :class="['player-item', { 'current-player': player.id === currentPlayerId }]"
      >
        <div class="player-info">
          <div class="player-avatar" :style="{ backgroundColor: player.color }">
            {{ (player.nickname || '玩家').charAt(0).toUpperCase() }}
          </div>
          <div class="player-details">
            <div class="player-name">
              {{ player.nickname || '玩家' }}
              <span v-if="player.id === currentPlayerId" class="you-badge">你</span>
            </div>
            <div class="player-status">
              <span class="status-dot" :class="{ online: player.isOnline }"></span>
              {{ player.isOnline ? '在线' : '离线' }}
            </div>
          </div>
        </div>
        
        <div class="player-stats">
          <div class="area-stat">
            <span class="stat-label">面积:</span>
            <span class="stat-value">{{ player.area || 0 }}</span>
          </div>
          <div class="rank-stat" v-if="gameStatus === 'playing' || gameStatus === 'finished'">
            <span class="stat-label">排名:</span>
            <span class="stat-value rank-{{ player.rank || '-' }}">{{ player.rank || '-' }}</span>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 游戏倒计时 -->
    <div class="game-timer" v-if="gameStatus === 'playing'">
      <div class="timer-label">剩余时间</div>
      <div class="timer-display">
        {{ formatTime(remainingTime) }}
      </div>
    </div>
    
    <!-- 游戏结果 -->
    <div class="game-results" v-if="gameStatus === 'finished'">
      <h4>游戏结果</h4>
      <div class="winner-info" v-if="winner">
        <div class="winner-badge">🏆 获胜者</div>
        <div class="winner-name">{{ winner.nickname || '玩家' }}</div>
        <div class="winner-area">占领面积: {{ winner.area }} 像素</div>
      </div>
      
      <div class="final-rankings">
        <h5>最终排名</h5>
        <div 
          v-for="(player, index) in sortedPlayers" 
          :key="player.id"
          :class="['ranking-item', { 'winner': player.id === winner?.id }]"
        >
          <span class="ranking-position">{{ index + 1 }}</span>
          <span class="ranking-name">{{ player.nickname || '玩家' }}</span>
          <span class="ranking-area">{{ player.area || 0 }} 像素</span>
        </div>
      </div>
    </div>
    
    <!-- 游戏控制按钮 -->
    <div class="game-controls">
      <button 
        v-if="gameStatus?.toLowerCase() === 'waiting'"
        @click="startGame"
        :disabled="players.length < 2"
        class="control-btn start-btn"
      >
        开始游戏
      </button>
      <button 
        v-if="gameStatus?.toLowerCase() === 'waiting'"
        @click="startSoloTest"
        class="control-btn solo-btn"
      >
        单人测试（加入测试玩家并开始）
      </button>
      
      <button 
        v-if="gameStatus === 'finished'"
        @click="restartGame"
        class="control-btn restart-btn"
      >
        重新开始
      </button>
      
      <button 
        v-if="gameStatus === 'playing'"
        @click="forceEndGame"
        class="control-btn force-end-btn"
      >
        强制终止比赛
      </button>
      
      <button 
        @click="leaveRoom"
        class="control-btn leave-btn"
      >
        离开房间
      </button>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  players: {
    type: Array,
    default: () => []
  },
  maxPlayers: {
    type: Number,
    default: 6
  },
  gameStatus: {
    type: String,
    default: 'waiting' // waiting, playing, finished
  },
  remainingTime: {
    type: Number,
    default: 0
  },
  currentPlayerId: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['start-game', 'restart-game', 'leave-room', 'start-solo-test', 'force-end-game'])

// 计算属性
const sortedPlayers = computed(() => {
  return [...props.players].sort((a, b) => {
    // 按面积降序排列
    const areaA = a.area || 0
    const areaB = b.area || 0
    if (areaA !== areaB) {
      return areaB - areaA
    }
    // 面积相同时按加入时间排序
    return new Date(a.joinedAt) - new Date(b.joinedAt)
  })
})

const winner = computed(() => {
  if (props.gameStatus?.toLowerCase() !== 'finished') return null
  return sortedPlayers.value[0]
})

const gameStatusText = computed(() => {
  const status = props.gameStatus?.toLowerCase()
  switch (status) {
    case 'waiting': return '等待中'
    case 'playing': return '游戏中'
    case 'finished': return '已结束'
    default: return '未知状态'
  }
})

// 格式化时间
const formatTime = (seconds) => {
  const minutes = Math.floor(seconds / 60)
  const remainingSeconds = seconds % 60
  return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`
}

// 事件处理
const startGame = () => {
  if (props.players.length >= 2 && props.gameStatus?.toLowerCase() === 'waiting') {
    emit('start-game')
  }
}

const startSoloTest = () => {
  if (props.gameStatus?.toLowerCase() === 'waiting') {
    emit('start-solo-test')
  }
}

const restartGame = () => {
  emit('restart-game')
}

const forceEndGame = () => {
  emit('force-end-game')
}

const leaveRoom = () => {
  emit('leave-room')
}
</script>

<style scoped>
.player-list-container {
  background: white;
  border-radius: 12px;
  border: 1px solid #e9ecef;
  padding: 20px;
  min-width: 280px;
  max-width: 320px;
  height: fit-content;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e9ecef;
}

.section-header h3 {
  margin: 0;
  font-size: 18px;
  color: #495057;
}

.game-status {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.game-status.waiting {
  background: #fff3cd;
  color: #856404;
}

.game-status.playing {
  background: #d4edda;
  color: #155724;
}

.game-status.finished {
  background: #f8d7da;
  color: #721c24;
}

.players-container {
  margin-bottom: 20px;
}

.player-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  margin-bottom: 8px;
  border-radius: 8px;
  border: 1px solid #e9ecef;
  transition: all 0.2s;
}

.player-item:hover {
  background: #f8f9fa;
}

.player-item.current-player {
  border-color: #007bff;
  background: #f0f8ff;
}

.player-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.player-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 16px;
}

.player-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.player-name {
  font-weight: 500;
  color: #495057;
  display: flex;
  align-items: center;
  gap: 8px;
}

.you-badge {
  background: #007bff;
  color: white;
  padding: 2px 6px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: normal;
}

.player-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #6c757d;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #dc3545;
}

.status-dot.online {
  background: #28a745;
}

.player-stats {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.area-stat,
.rank-stat {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
}

.stat-label {
  color: #6c757d;
}

.stat-value {
  font-weight: 500;
  color: #495057;
}

.rank-1 { color: #ffd700; }
.rank-2 { color: #c0c0c0; }
.rank-3 { color: #cd7f32; }

.game-timer {
  text-align: center;
  padding: 16px;
  margin: 16px 0;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.timer-label {
  font-size: 14px;
  color: #6c757d;
  margin-bottom: 8px;
}

.timer-display {
  font-size: 24px;
  font-weight: bold;
  color: #dc3545;
  font-family: 'Courier New', monospace;
}

.game-results {
  margin: 16px 0;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.game-results h4 {
  margin: 0 0 12px 0;
  color: #495057;
  text-align: center;
}

.winner-info {
  text-align: center;
  margin-bottom: 16px;
  padding: 12px;
  background: #fff3cd;
  border-radius: 8px;
  border: 1px solid #ffeaa7;
}

.winner-badge {
  font-size: 16px;
  margin-bottom: 8px;
}

.winner-name {
  font-size: 18px;
  font-weight: bold;
  color: #856404;
  margin-bottom: 4px;
}

.winner-area {
  font-size: 14px;
  color: #856404;
}

.final-rankings h5 {
  margin: 0 0 8px 0;
  color: #495057;
}

.ranking-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #e9ecef;
}

.ranking-item:last-child {
  border-bottom: none;
}

.ranking-item.winner {
  background: #fff3cd;
  border-radius: 4px;
  padding: 8px;
  margin: 0 -8px;
}

.ranking-position {
  font-weight: bold;
  color: #495057;
  min-width: 20px;
}

.ranking-name {
  flex: 1;
  margin: 0 12px;
  color: #495057;
}

.ranking-area {
  font-size: 12px;
  color: #6c757d;
}

.game-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 16px;
}

.control-btn {
  padding: 10px 16px;
  border: none;
  border-radius: 6px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.control-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.start-btn {
  background: #28a745;
  color: white;
}

.start-btn:hover:not(:disabled) {
  background: #218838;
}

.restart-btn {
  background: #17a2b8;
  color: white;
}

.restart-btn:hover {
  background: #138496;
}

.leave-btn {
  background: #dc3545;
  color: white;
}

.leave-btn:hover {
  background: #c82333;
}

.solo-btn {
  background: #6c757d;
  color: white;
}

.solo-btn:hover {
  background: #5a6268;
}

.force-end-btn {
  background: #ffc107;
  color: #212529;
}

.force-end-btn:hover {
  background: #e0a800;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .player-list-container {
    min-width: auto;
    max-width: none;
    width: 100%;
  }
  
  .player-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .player-stats {
    align-items: flex-start;
    width: 100%;
  }
  
  .game-controls {
    flex-direction: row;
  }
  
  .control-btn {
    flex: 1;
  }
}
</style>
